/**
 * Apr 26, 2010
 */
package org.tomale.id.projects.ui.preferences;

import java.util.List;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.osgi.service.prefs.BackingStoreException;
import org.tomale.id.common.KeyValuePair;
import org.tomale.id.projects.Activator;
import org.tomale.id.projects.ProjectManager;

/**
 * @author ferd
 *
 */
public class ProjectsPage extends PreferencePage implements
		IWorkbenchPreferencePage {

	Combo _cboProviders;
	List<KeyValuePair<String, String>> _providers;
	
	public ProjectsPage(){
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Composite comp = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		// provider
		l = new Label(comp, SWT.NONE);
		l.setText("Projects data provider");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_cboProviders = new Combo(comp, SWT.DROP_DOWN | SWT.READ_ONLY);
		_cboProviders.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		
		initProviders();
		
		setPreferences();
		
		return comp;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub

	}
	
	private void initProviders(){
		_providers = ProjectManager.getProviders();
		if(_providers == null) {
			setErrorMessage("Unable to retrieve project providers.");
			Activator.logError("Unable to retrieve project providers.");
		} else {
			for(KeyValuePair<String, String> kv : _providers){
				_cboProviders.add(kv.getValue());
			}				
		}
	}
	
	private boolean valid(){
		return true;
	}
	
	private void save() throws BackingStoreException {
		if(valid()){
			IPreferenceStore store = getPreferenceStore();
			store.setValue(PreferenceConstants.PROVIDER_ID,
					getPairByValue(_providers, _cboProviders.getText()).getKey());
		}
	}
	
	private void setPreferences(){
		IPreferenceStore store = getPreferenceStore();
		if(!store.getString(PreferenceConstants.PROVIDER_ID).isEmpty()){
			_cboProviders.setText(getPairByKey(_providers, 
					store.getString(PreferenceConstants.PROVIDER_ID)).getValue());
		}
	}
	
	@Override
	public boolean performOk() {
		try {
			save();
			return super.performOk();
		}catch(BackingStoreException e){
			setErrorMessage(e.getMessage());
			return false;
		}
	}
	
	private KeyValuePair<String, String> getPairByKey(final List<KeyValuePair<String, String>> pairs,
			final String key){
			for(KeyValuePair<String, String> kv : pairs){
				if(key.equals(kv.getKey())){
					return kv;
				}
			}
			return null;
		}
		
	private KeyValuePair<String, String> getPairByValue(final List<KeyValuePair<String, String>> pairs,
		final String value){
		for(KeyValuePair<String, String> kv : pairs){
			if(value.equals(kv.getValue())){
				return kv;
			}
		}
		return null;
	}
}
